package com.itheima.hello.UDP通信;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws Exception {
        //创建服务端对象(Server)接收数据
        System.out.println("服务端启动");
        DatagramSocket server = new DatagramSocket(8888);

        //创建一个数据包对象
        byte[] bytes = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length);

        //接收数据
        while (true) {
            server.receive(packet);
            //处理数据

            //记录本次接收了多少数据
            int len = packet.getLength();

            String rs = new String(bytes,0,len);
            System.out.println(rs);

            System.out.println(packet.getAddress().getHostAddress());//拿到客户端ip地址
            System.out.println(packet.getPort());//拿到客户端端口号
            System.out.println("服务端接收数据完毕");
            System.out.println("---------------------------------------");
        }


    }
}
